package mywebapp.dao.visiteur;

import mywebapp.dao.Champ;
import mywebapp.dao.ETypeSimple;
import mywebapp.dao.Entity;
import mywebapp.dao.TypeSimple;
import mywebapp.dao.impl.TypeEntityImpl;
import mywebapp.dao.impl.TypeSimpleImpl;

public class VisiteurModelJava implements VisiteurEntity{

	public static String syntaxeSetGetParam(String champ)
	{
		champ = champ.trim().toLowerCase().replaceAll(" ", "_");
		return champ.replaceFirst(champ.toCharArray()[0]+"", (champ.toCharArray()[0]+"").toUpperCase());
	}
	
	public static String syntaxeParam(String champ)
	{
		champ = champ.trim().toLowerCase().replaceAll(" ", "_");
		return champ;
	}
	
	@Override
	public String visiter(Entity e) {
		String classContenue = "";
		classContenue += "public class " + VisiteurModelJava.syntaxeSetGetParam(e.getName()) + " {\n\n";

		classContenue += "\tprivate Long ID;\n\n";
		classContenue += "\tpublic Long getID(){ return this.ID;}\n\n";
		classContenue += "\tpublic void setID(Long value){ this.ID = value;}\n\n";

		for(Object  o1 : e.getChamps()){
			String nameType = "";
			Champ c = (Champ) o1;

			if(c.getType() instanceof TypeSimple){
				TypeSimpleImpl t = (TypeSimpleImpl) c.getType();
				switch(t.getType().getValue()){
				case ETypeSimple.BIGINT :
					nameType = "long" ;
					break;
				case ETypeSimple.STRING :
					nameType =  "String";
					break;
				case ETypeSimple.DATE:
					classContenue = "import java.util.Date;\n" + classContenue;
					nameType = "Date";
					break;
				case ETypeSimple.FLOAT:
					nameType = "float";
					break;
				case ETypeSimple.INT:
					nameType = "int";
					break;
				}
			}else{
				TypeEntityImpl t = (TypeEntityImpl) c.getType();
				nameType = VisiteurModelJava.syntaxeSetGetParam(t.getType().getName());
			}


			if(c.isIsList()){
				classContenue = "import java.util.ArrayList;\n" + classContenue;
				if(c.getType() instanceof TypeEntityImpl)
					nameType = "ArrayList<"+nameType+">";
				else
					nameType = "ArrayList";
			}

			classContenue += "\tprivate "+ nameType + " " + VisiteurModelJava.syntaxeParam(c.getName()) + ";\n\n";
			classContenue += "\tpublic "+ nameType+ " get"+ VisiteurModelJava.syntaxeSetGetParam(c.getName()) +"(){ return this."+ VisiteurModelJava.syntaxeParam(c.getName()) +" ;}\n\n";
			classContenue += "\tpublic void set"+ VisiteurModelJava.syntaxeSetGetParam(c.getName()) +"("+nameType + " value){ this."+ VisiteurModelJava.syntaxeParam(c.getName()) +" = value;}\n\n";
		}
		//ajout de reset
		classContenue += "\tpublic void reset(){\n";
		classContenue += "\t\tthis.ID = (long) 0;\n";	
		for(Object  o1 : e.getChamps()){
			Champ c = (Champ) o1;
			if(c.isIsList()){
				classContenue += "\t\tthis."+ VisiteurModelJava.syntaxeParam(c.getName()) +".clear();\n";
			}else{
				if(c.getType() instanceof TypeSimple){
					TypeSimpleImpl t = (TypeSimpleImpl) c.getType();
					switch(t.getType().getValue()){
					case ETypeSimple.BIGINT :
						classContenue += "\t\tthis."+ VisiteurModelJava.syntaxeParam(c.getName()) +" = (long)0;\n";
						break;
					case ETypeSimple.FLOAT :
						classContenue += "\t\tthis."+ VisiteurModelJava.syntaxeParam(c.getName()) +" = (float)0;\n";
						break;
					case ETypeSimple.INT :
						classContenue += "\t\tthis."+ VisiteurModelJava.syntaxeParam(c.getName()) +" = 0;\n";
						break;
					case ETypeSimple.STRING :
						classContenue += "\t\tthis."+ VisiteurModelJava.syntaxeParam(c.getName()) +" = \"\";\n";
						break;
					case ETypeSimple.DATE:
						break;
					}
				}else{
					classContenue += "\t\tthis."+ VisiteurModelJava.syntaxeParam(c.getName()) +" = new " + VisiteurModelJava.syntaxeSetGetParam(c.getName()) + "();\n";	
				}
			}
		}
		classContenue += "\t}\n";
		classContenue += "}";
		return classContenue;
	}

}
